<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>15-2 重写数组Symbol.iterator属性</title>
</head>

<body>
  <script>
    let arr = ['a', 'b', 'c'];

    arr[Symbol.iterator] = function () {
      let self = this, index = 0;
      return {
        next() {
          return index >= self.length ? {
            done: true,
            value: 'undefined'
          } : {
              done: false,
              value: self[index++]
            }
        }
      }
    }

    for (const key of arr) {
      console.log(key) //=> a b c
    }
  </script>
</body>

</html>